<template>
	<view class="content">
		<heade title="客户参数" cross bg_color="bg_fff">
			<!-- <view class="f-s15 f-w800">清空</view> -->
			<text class="iconfont icon-jiahao_o f-s20 m-l5 f-wbold" @tap="$common.url('/pages/parameters/addClient')"></text>
		</heade>
		<view v-if="!classify">
			<view :style="{'top':($common.headHeight() + 43) + 'px'}" class="position-fixed box_box d-flex bg_fff align-center p-l20 p-r20 p-b10 p-t5" style="border-bottom: 2rpx solid #F8F8F8;width: 100%;z-index: 9;">
				<view class="d-flex align-center" @click="classify = !classify">
					<view class="f-s16">分类</view>
					<view class="iconfont icon-sanjiaoxing f-s10 m-l10"></view>
				</view>
				<view class="sousuo flex1 border-r12 m-l15 d-flex align-center">
					<view class="iconfont icon-sousuo f-s14 m-l10"></view>
					<input type="text" class="f-s14 m-l10 flex1" :adjust-position="false" v-model="Name" @input="inputClick" placeholder="搜索客户名称">
				</view>
				<!-- <view class="iconfont icon-erweima f-s20"></view> -->
			</view>
			<view style="height: 82rpx;"></view>
			<view class="">
				<view class="position-center text-center" v-if="list.length == 0">
					<view class="iconfont icon-zhanghao f-s60 co_999"></view>
					<view class="m-t10 f-s14 co_999 text-center">暂无数据</view>
				</view>
				<view v-for="item in list" @tap="urlLink(item) " class=" d-flex align-center p-l15 p-r15  m-t15 p-b15 justify-content-space-between "  style="border-bottom: 2rpx solid #F8F8F8;">
					<view class=" ">
						<view class="f-s16" v-if="item.name">{{item.name}}<text  v-if="item.partnerAddresDTOs[0]">({{item.partnerAddresDTOs[0].mobilePhone}})</text></view>
						<view class="d-flex align-center f-s14 m-t15 co_333" v-if="item.partnerAddresDTOs[0]">
							<view  class="m-r5">{{item.partnerAddresDTOs[0].contact}}</view>
							<view >{{item.partnerAddresDTOs[0].telephoneNo}}</view>
						</view>
						<view class="f-s14 m-t5 co_999" v-if="item.partnerAddresDTOs[0]">{{item.partnerAddresDTOs[0].shipmentAddress}}</view>
					</view>
					<view class="iconfont icon-dianhuatianchong f-s25 co_999" v-if="item.partnerAddresDTOs[0]" @tap.stop="$common.phone(item.partnerAddresDTOs[0].mobilePhone)"></view>
				</view>
				<uni-load-more v-if="more" :status="status"></uni-load-more>
			</view>
		</view>
		
		<view class="" v-if="classify" class=" box_box bg_fff m-b10" style="border-top: 2rpx solid #e8e8e8;width: 100%;z-index: 9;overflow-y: auto;">
			<view class="f-s16">
				<view class="m-l12  p-t10 p-b10" @tap="subset()">全部</view>
				<view class="" >	
					<view v-for="(item,i) in kehuList">
						<view class="p-l20 p-t10 p-b10 bg_f6f6f6" @click="kehuClick(i,item)">{{item.name}}({{item.sub.length}})</view>
						<view class="" v-for="(item2,j) in item.sub">
							<view class="p-l30 p-t10 p-b10 p-t10 p-b10 bg_fafafa"  @click="stairClick(i,j,item2)" v-if="item.stair">{{item2.name}}({{item2.sub.length}})</view>
							<view class="p-l40 p-t10 p-b10 p-t10 p-b10 bg_fafafa" @tap="subset(item3)" v-if="item2.headset" v-for="item3 in item2.sub">{{item3.name}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="b-liuhai" style="background-color: #F3F2F7;"><view style="height: 92rpx;"></view></view>
			<view style="position: fixed; bottom: 0;width: 100%;z-index: 9;" class="bg_fff" >
				<view class="b-liuhai d-flex  align-center" style="">
					<view class="f-w500 f-s17 text-center box_box bg_fff" style="width: 50%; height: 92rpx;line-height: 92rpx;border-top: 2rpx solid #DFDFDF;" @tap="saveClick">返回</view>
					<view class="f-w500 co_fff f-s17 text-center bg_4494EE	" style="width: 50%; height: 92rpx;line-height: 92rpx;" @tap="saveClick">收起</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import {
		executeSQL,
	} from '@/common/sqlite.js';
	export default {
		data() {
			return {
				list:[],
				classify:false,
				Code:'',
				Name:'',
				isStatus:false,
				title:'',
				screenHeight:'',
				kehuList:[],
				PartnerClass:{
					Code:'',
					Name:''
				},
				more:false,
				status:'noMore',
				pageIndex:1,
				ts:''
			}
		},
		onLoad(e) {
			if(e.status) {
				this.isStatus = e.status
			}
			if(e.title) {
				this.title = e.title
			}
			let _this = this
			uni.getSystemInfo({
				success: function (res) {
					_this.screenHeight = res.screenHeight
				}
			});
			uni.showLoading({
				title: '加载中'
			});
			// 获取用户分类
			executeSQL('xiaoap', 'select * from kehuList_1', res => {
				res.forEach(e => {
					e.sub = JSON.parse(e.sub)
				})
				let value = res
				value.forEach((e,i) => {
					e.stair = false
					value[i].sub.forEach(j => {
						j.headset = false
					})
				})
				this.kehuList = value
				uni.hideLoading();
			})
				
			this.getData1()
		},
		onReachBottom(){
			var _this = this;
			if(_this.status == 'loading'){
				_this.pageIndex++
				_this.more=true;
				this.getData1()
			}
		},
		methods: {
			kehuClick(i,item) {
				this.kehuList[i].stair = !this.kehuList[i].stair
				this.kehuList[i].sub.forEach(e => {
					if(item.name == e.name) {
						this.kehuList[i].stair = !this.kehuList[i].stair
					}else {
						e.headset = false
					}
				})
				
			},
			stairClick(i,j,item) {
				this.kehuList[i].sub.forEach(e => {
					if(item.name == e.name) {
						this.kehuList[i].sub[j].headset = !this.kehuList[i].sub[j].headset
					}else {
						e.headset = false
					}
				})
				
			},
			subset(item) {
				if(item) {
					this.PartnerClass = {
						Code:item.code,
						Name:item.name
					}
				}else {
					this.PartnerClass = {
						Code:'',
						Name:''
					}
				}
				this.classify = false
				this.pageIndex = 1
				this.getData1()
			},
			// 搜索客户
			inputClick() {
				this.pageIndex = 1
				this.list = []
				this.getData1()
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 100
				}); 
			},
			getData1() {
				let sql = `select * from theCustomer1  limit ${(this.pageIndex-1) * 10},10`
				let temp = 'where 1=1 ';
				if(this.Name != ''){
					temp += `and name like '%${this.Name}%' or partnerAddresDTOs like '%${this.Name}%' ` ;
				}
				if(this.PartnerClass.Code != ''){
					temp += `and partnerClass like '%${this.PartnerClass.Code}%' `;
				}
				sql = `select * from theCustomer1 ${temp} limit ${(this.pageIndex-1) * 10},10`
				
				if(this.$db.get('default_config').partner_scope == 1) {
					sql = `select * from theCustomer1 where priuserdefdecm6 like '%1%' limit ${(this.pageIndex-1) * 10},10`
				}
				executeSQL('xiaoap', sql, res => {
					if(res.length > 0) {
						res.forEach(e => {
							e.partnerType = JSON.parse(e.partnerType)
							e.partnerClass = JSON.parse(e.partnerClass)
							e.settlementPartner = JSON.parse(e.settlementPartner)
							e.saleDepartment = JSON.parse(e.saleDepartment)
							e.priceGrade = JSON.parse(e.priceGrade)
							e.partnerAddresDTOs = JSON.parse(e.partnerAddresDTOs)
						})
					}
					if(this.pageIndex>1){
						this.status = 'loading'
						this.list = this.list.concat(res||[])
					}else{
						this.status = 'loading'
						this.list = res;
					}
					if(res.length==0){
						this.status = 'noMore';
						this.more= true;
						return;
					}
					this.more= false;
				})
			},
			urlLink(item) {
				if(this.isStatus) {
					var pages = getCurrentPages();
					var prevPage = pages[pages.length - 2]; //上一个页面
					if(this.title == '结算客户'){
						prevPage.$vm.closeAccount = item
					}else {
						prevPage.$vm.client = item
					}
					uni.navigateBack()
				}else {
					if(this.title == '销售订单') {
						this.$common.url(`/pages/paceOrder/paceOrder?client=${JSON.stringify(item)}&title=${this.title}`)
					}else if(this.title == '退货单') {
						this.$common.url(`/pages/paceOrder/returnGoods?client=${JSON.stringify(item)}&title=${this.title}`)
					}
				}
			},
			saveClick() {
				this.classify = false
			},
		}
	}
</script>

<style lang="scss">
	.sousuo {
		width: 490rpx;
		height: 60rpx;
		background: #F3F2F7;
	}
</style>
